package com.example.zhyq.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.entity.ParkBeanItem
import com.example.base.mvvm.BaseActivity
import com.example.zhyq.R
import com.example.zhyq.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * 车位
 */

@Route(path = "/app/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding,ParkViewModel>() {

    val parkPageAdapter by lazy { ParkPageAdapter() }
    var areaId = "-1"
    var type = ""
    override fun initView() {
        binding.rvPark.apply {
            adapter = parkPageAdapter
            layoutManager=LinearLayoutManager(this@ParkActivity)
        }


        binding.headPark.headerTitle.text = "车位列表"
        binding.headPark.headerBack.setOnClickListener {
            finish()
        }
        binding.headPark.headerAdd.setImageResource(R.drawable.baseline_add_24)
        binding.headPark.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/AddParkActivity").navigation()
        }


        //点击进入详情
        parkPageAdapter.setOnItemClickListener(object :ParkPageAdapter.OnItemClickListener{
            override fun onItemClick(position: Int, item: ParkBeanItem) {
                Log.i("===", "$position ---$item")
                ARouter.getInstance().build("/app/ParkEditActivity").withSerializable("bean",item).navigation()

            }

        })

        //长按
        parkPageAdapter.setOnItemLongClickListener(object :ParkPageAdapter.OnItemLongClickListener{
            override fun onItemLongClick(item: ParkBeanItem) {
                //ARouter.getInstance().build("/app/ParkVRActivity").navigation()
                vm.deletePark(item.id)
            }

        })

        val helper =
            ItemTouchHelper(object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
                override fun onMove(
                    recyclerView: RecyclerView,
                    viewHolder: RecyclerView.ViewHolder,
                    target: RecyclerView.ViewHolder
                ): Boolean {
                    return false
                }

                override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                    val position = viewHolder.adapterPosition
                    parkPageAdapter.notifyItemRemoved(position)

                }

            })
        helper.attachToRecyclerView(binding.rvPark)

        binding.parkFilter.setOnClickListener {
            binding.drawer.openDrawer(GravityCompat.END)
        }

        binding.drawRg.setOnCheckedChangeListener { radioGroup, i ->
            binding.drawer.closeDrawer(GravityCompat.END)
            var status = ""
            if (i==R.id.rb_kx) {
                status="空闲"
            }else if (i==R.id.rb_zy) {
                status="已占用"
            }else {
                status="维护中"
            }
            lifecycleScope.launch {
                vm.getParkByType(status,"状态").collect{
                    parkPageAdapter.submitData(it)
                }

            }
        }

        binding.parkSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                areaId = query!!
                if (type.isNotEmpty()) {
                    lifecycleScope.launch {
                        vm.getParkByType(areaId,type).collect {
                            parkPageAdapter.submitData(it)
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
               return true
            }

        })

        binding.drawArea.setOnClickListener {
            type = "区域"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
            if (areaId=="-1") {
                ToastUtils.showLong("请输入区域ID")
                return@launch
            }
                vm.getParkByType(areaId,type).collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }

        binding.drawType.setOnClickListener {
            type = "类型"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if (areaId=="-1") {
                    ToastUtils.showLong("请输入类型")
                    return@launch
                }
                vm.getParkByType(areaId,type).collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }
    }

    override fun initData() {
        vm.deleteParkSuc.observe(this) {
            ToastUtils.showLong("删除成功")
            lifecycleScope.launch {
                vm.getParkByType("标准车位","类型").collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch {
            vm.getParkByType("标准车位","类型").collect{
                parkPageAdapter.submitData(it)
            }
        }


    }
}